package com.nextel.project.pedido.bean;

import pe.com.nextel.rms.DataInputStreamX;
import pe.com.nextel.rms.DataOutputStreamX;
import pe.com.nextel.rms.PersistenteX;
import java.io.IOException;



import org.kxml2.io.KXmlParser;
import org.kxml2.io.KXmlSerializer;
import pe.com.nextel.util.ArrayList;
import pe.com.nextel.util.Comparable;
import pe.com.nextel.util.Iterator;
import pe.com.nextel.util.List;
import pe.com.nextel.util.StringUtils;

/**
 *
 *  
 */
public class ArticuloPorCodigoBean extends PersistenteX implements Comparable {
    
    private int idRecordArticulo;
    private short posRecordArticulo;
    private String codigoArticulo;
    
    public void persistir(DataOutputStreamX dataOutput, List list) throws IOException {
        dataOutput.writeInt(list.size( ));
        for (Iterator it = list.iterator(); it.hasNext();) {
            ArticuloPorCodigoBean articuloCodigo = (ArticuloPorCodigoBean) it.next();
            dataOutput.writeInt(articuloCodigo.getIdRecordArticulo());
            dataOutput.writeShort(articuloCodigo.getPosRecordArticulo());
            dataOutput.writeString(StringUtils.stringNullToBlank(articuloCodigo.getCodigoArticulo()));
        }
    }
    
    public List recuperar(DataInputStreamX dataInput) throws IOException {
        List lista = new ArrayList();
        ArticuloPorCodigoBean articuloCodigo = null;
        int cant = dataInput.readInt();

        for (int i = 0; i < cant; i++) {
        
            articuloCodigo = new ArticuloPorCodigoBean( );
            
            articuloCodigo.setIdRecordArticulo(dataInput.readInt( ));
            
            articuloCodigo.setPosRecordArticulo(dataInput.readShort( ));
            
            articuloCodigo.setCodigoArticulo(dataInput.readString( ));
            
            lista.add(articuloCodigo);
            
        }

        return lista;
    }
    
    public int getIdRecordArticulo() {
        return idRecordArticulo;
    }
    
    public void setIdRecordArticulo(int idRecordArticulo) {
        this.idRecordArticulo = idRecordArticulo;
    }
    
    public short getPosRecordArticulo() {
        return posRecordArticulo;
    }
    
    public void setPosRecordArticulo(short posRecordArticulo) {
        this.posRecordArticulo = posRecordArticulo;
    }
    
    public String getCodigoArticulo() {
        return codigoArticulo;
    }
    
    public void setCodigoArticulo(String codigoArticulo) {
        this.codigoArticulo = codigoArticulo;
    }
    
    public void writeXMLObject(KXmlSerializer writer) throws IOException {
    }
    
    public void readXMLObject(KXmlParser parser) throws IOException {
        setIdRecordArticulo(Integer.parseInt(parser.getAttributeValue(null, "i")));
        setPosRecordArticulo(Short.parseShort(parser.getAttributeValue(null, "p")));
        setCodigoArticulo(parser.getAttributeValue(null, "c"));
    }
    
    //retorna negativo <0 -> this es menor al arg0
    //retorna 0 -> this = arg0
    //retorna positivo >0 -> this es mayor al arg0
    public int compareTo(Object o) {
        try {
            
            ArticuloPorCodigoBean bean = (ArticuloPorCodigoBean)o;
            
//            int x = this.getCodigoArticulo().length();
//            int y = bean.getCodigoArticulo().length();
            
//            if( x == y ){
            return  this.getCodigoArticulo().compareTo(bean.getCodigoArticulo());
//            }else if( x > y ){
//                return 1;
//            }else{
//                return -1;
//            }
            
        } catch (Exception e) {
            return  -1;
        }
    }
    
    public boolean equals(Object o) {
        try {
            ArticuloPorCodigoBean bean = (ArticuloPorCodigoBean)o;
            if(this.getCodigoArticulo().equals(bean.getCodigoArticulo()) ) return true;
            else return  false;
        } catch (Exception e) {
            return  false;
        }
    }
    
    public int hashCode( ) {
        if(getCodigoArticulo() != null)
            return getCodigoArticulo().hashCode();
        else
            return 0;
    }
    
    public void clear(){
        
    }
}
